package com.luodea.rabbitmqconsume;

import com.luodea.common.RabbitMqConfig;
import com.luodea.common.User;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.AcknowledgeMode;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.listener.RabbitListenerEndpointRegistry;
import org.springframework.amqp.support.AmqpMessageHeaderAccessor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.messaging.handler.annotation.Headers;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.messaging.support.MessageHeaderAccessor;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Map;

/**
 * @author luodea
 * @date 2020/1/31 09:00
 * @description
 */
@Component
@Slf4j
@RabbitListener(queues = RabbitMqConfig.DIRECT_QUEUE_1,returnExceptions = "false",ackMode = "MANUAL")
public class RabbitMqConsume {



    @RabbitHandler
    public void consumeData(
            /**
             * 接收当前连接的通道
             */
            com.rabbitmq.client.Channel channel,
            /**
             * 接收消息对象（包含消息头、消息体、和相应的属性）
             */
            org.springframework.amqp.core.Message message,
            /**
             * 接收消息头和消息体
             */
            org.springframework.messaging.Message message1,
            /**
             * 接收消息头数据对象
             */
            org.springframework.messaging.MessageHeaders messageHeaders,
            /**
             * 接收可修改属性的基类
             */
            org.springframework.messaging.support.MessageHeaderAccessor messageHeaderAccessor,
            /**
             * 接收可修改AMQP规范源生属性
             */
            org.springframework.amqp.support.AmqpMessageHeaderAccessor amqpMessageHeaderAccessor,
            /**
             * 接收消息体
             */
            @Payload User user,
            /**
             * 接收消息头指定的属性
             */
            @Header(name = "amqp_deliveryTag") Long deliveryTag,
            /**
             * 接收消息头的所有属性
             */
            @Headers Map  headers) throws IOException {
//            String msg = new String(payload);
            log.info("=====>"+Thread.currentThread().getName());
            log.info("监听"+RabbitMqConfig.DIRECT_QUEUE_1+"队列==>"+user.toString());
//            channel.basicAck(amqpMessageHeaderAccessor.getDeliveryTag(),true);
    }


    @RabbitListener(
            bindings = @QueueBinding(
                    value = @Queue(value = "MRO.updateBaseInfo.queue", durable = "true"),
                    exchange = @Exchange(name = "MRO_Exchange", type = ExchangeTypes.DIRECT, durable = "true"),
                    key = "baseInfoUpdate"
            ),
//      errorHandler = "errorHandler",
            returnExceptions = "true",
            errorHandler = "testRabbitListenerErrorHandler"
    )
    public String receiveLocationChangeMessage(String message){
        System.out.println(message);
        throw new RuntimeException("foo");
//        return "success";
    }



}
